# encoding = utf-8
from collections import Counter

from application.logging import logger


# @CodeTimingUtil(name="计算加权均值[室温][func_calc_weighted_temper]")
def func_calc_weighted_temper(list_temper):
    """
    室内温度加权均值 TODO func_weighted_temper
    :param list_temper:
    :return:
    """
    #
    # 温度列表: 取整map(int,...)后再转换成list
    int_temper_list = list(map(int, list_temper))

    #
    # 概率列表
    probability = []

    #
    # 出现次数: 每个元素
    int_temper_counter = Counter(int_temper_list)
    logger.info(f"Counter(int_temper):\n {int_temper_counter}")

    #
    # for ->
    for int_temper_item in int_temper_list:
        #
        # 出现概率: = 某一温度次数/总次数
        prob = round((int_temper_counter[int_temper_item] / len(int_temper_list)), 2)
        #
        # 加入列表: 概率列表
        probability.append(prob)
        pass  # <- for
    #
    # 温度 * 各自概率
    list3 = map(lambda x, y: x * y, int_temper_list, probability)  # 每个值乘以自己的概率

    #
    # 温度总和 / 概率总和
    return sum(list3) / sum(probability)

    pass


pass

if __name__ == '__main__':
    list_temper_ = [1.1, 2.2, 2.3, 3.4, 3.5, 3.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3, 5.4]
    print(list(map(int, list_temper_)))
    result_ = func_calc_weighted_temper(list_temper=list_temper_)
    logger.info(f"计算结果: {result_}")
    pass
